package com.lujieni.springbootemail.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lujieni.springbootemail.dto.AdminDTO;
import com.lujieni.springbootemail.dto.MailEntity;
import com.lujieni.springbootemail.dto.UserDTO;
import com.lujieni.springbootemail.service.MailService;
import com.lujieni.springbootemail.utils.DataDesensitizationUtils;
import freemarker.template.TemplateException;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.io.IOException;

/**
 * @Author：小鹿爱吃鱼
 * @Date: 2025/4/24 22:24
 * @Description:
 */
@RestController
@RequiredArgsConstructor
public class MailController {

    private final MailService mailService;

    @GetMapping("/sendSimpleMail")
    public void sendMail() {
        mailService.sendSimpleMail();
    }

    @PostMapping("/sendMailByFreemarker")
    public void sendMailByFreemarker(@RequestBody MailEntity mailEntity) throws MessagingException, TemplateException, IOException {
        mailService.sendMailByFreemarker(mailEntity);
    }

    @GetMapping("/showEmail")
    public String showEmail() {
        String encrypt = DataDesensitizationUtils.encrypt("982584989@qq.com");
        return encrypt;
    }

    @GetMapping("/convert")
    public UserDTO convert() {
        /*
            String 虽然是引用类型，但它是 不可变（Immutable） 的，修改 adminDTO.userName 会创建一个新字符串，
            而不会影响 bean.userName
         */
        AdminDTO adminDTO = new AdminDTO("lujieni", "12345");
        UserDTO bean = BeanUtil.toBean(adminDTO, UserDTO.class);
        adminDTO.setUserName("haha");
        return bean;
    }

}
